package com.fowo.api.sys.acm;

import com.fowo.api.common.model.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 异步控制器方法控制器
 * @author yl_ls
 */
@RequestMapping("/api/sys/acm/")
@RestController
public class AcmController {

    @Resource
    private AsyncControllerMethodExecutor methodExecutor;

    @GetMapping("{taskId}")
    public Object getTaskState(@PathVariable Long taskId) {
        AsyncControllerMethodExecutor.TaskInfo taskInfo = methodExecutor.getResult(taskId, true);
        if (!taskInfo.finish() && !taskInfo.cancelled()) {
            // 未完成的任务，还是在 data 上直接返回任务编号
            return R.data(taskId);
        }
        return taskInfo.result();
    }
}
